$(function() {
    let load = axios.create({
        baseURL: "http://localhost:3000/static/data/"

    })

    function loadListData() {
        let options = {
            url: "/ind.json"
        }
        load(options)
            .then((res) => {

                var data = res.data


                var kkk = []

                for (let i = 0; i < data[2].data.length; i++) {
                    kkk.push(data[2].data[i])

                    $(".congoods").append(` <div class="a2 bos b${i}">
                        <img src=" ${data[2].data[i].majorPics}" alt="">
                        <p class="productname">${data[2].data[i].name}</p>
                        <p><span class="jianyi">官方建议零售价 </span><span class="price">￥${data[2].data[i].price}</span></p>
                             <P class="vipp">
                         <span class="sp1">登录</span>
                         <span class="sp2">注册</span>
                         <b class="sp3">即可享受会员价</b>
                         </P>
                    </div>`);
                }

                for (let i = 0; i < data[4].data.length; i++) {
                    kkk.push(data[4].data[i])

                    $(".congoods").append(` <div  class="a4 bos b${i}">
                        <img src=" ${data[4].data[i].pic}" alt="">
                        <p class="productname">${data[4].data[i].name}</p>
                        <p><span class="jianyi">官方建议零售价 </span><span class="price">￥${data[4].data[i].price}</span></p>
                           <P class="vipp">
                         <span class="sp1">登录</span>
                         <span class="sp2">注册</span>
                         <b class="sp3">即可享受会员价</b>
                         </P>
                    </div>`);
                }

                for (let i = 0; i < data[6].data.length; i++) {
                    kkk.push(data[6].data[i])

                    $(".congoods").append(` <div  class="a6 bos b${i}">
                        <img src=" ${data[6].data[i].pic}" alt="">
                        <p class="productname">${data[6].data[i].name}</p>
                        <p><span class="jianyi">官方建议零售价 </span><span class="price">￥${data[6].data[i].price}</span></p>
                             <P class="vipp">
                         <span class="sp1">登录</span>
                         <span class="sp2">注册</span>
                         <b class="sp3">即可享受会员价</b>
                         </P>
                    </div>`);
                }

                for (let i = 0; i < data[8].data.length; i++) {
                    kkk.push(data[8].data[i])
                    $(".congoods").append(` <div  class="a8 bos b${i}">
                        <img src=" ${data[8].data[i].pic}" alt="">
                        <p class="productname">${data[8].data[i].name}</p>
                        <p><span class="jianyi">官方建议零售价 </span><span class="price">￥${data[8].data[i].price}</span></p>
                            <P class="vipp">
                         <span class="sp1">登录</span>
                         <span class="sp2">注册</span>
                         <b class="sp3">即可享受会员价</b>
                         </P>
                    </div>`);

                }
                let TOKEN = cookie("TOKEN")
                console.log(TOKEN);
                if (TOKEN.length > 0) {
                    $(".vipp").addClass("hide")
                } else {
                    console.log("没有");
                }



                // 懒加载


                var $tz = $(".bos")
                $tz.each(function(i, item) {
                    $(item).on("click", function() {
                        console.log($(item).attr("class"));
                        location.href = "./detial.html" + "#" + $(item).attr("class")

                    })
                })
            });
    }

    loadListData();

})

class lazyLoad {
    constructor(selector) {
        console.log(this)
        this.imgs = Array.from(document.querySelectorAll(selector))
        this.top_offset = this.imgs.map((item) => this.getAbsoluteTop(item))
        this.bindEvent()
        window.dispatchEvent(new Event("scroll"))
    }
    bindEvent() {
        window.addEventListener("scroll", () => {
            for (let i = 0; i < this.top_offset.length; i++) {
                if (this.top_offset[i] <= scrollY + innerHeight) {
                    let img = new Image()
                    img.src = this.imgs[i].getAttribute("data-src")
                    img.onload = () => {
                        this.imgs[i].src = this.imgs[i].getAttribute("data-src")

                    }
                }
            }
        })
    }

    getAbsoluteTop(dom) {
        if (dom.offsetParent !== document.body) {
            return dom.offsetTop + this.getAbsoluteTop(dom.offsetParent)
        }
        return dom.offsetTop
    }
}